package top.kuxiao.crmserver.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.kuxiao.crmserver.service.StatementService;
import top.kuxiao.crmserver.utils.Auth;
import top.kuxiao.crmserver.utils.ResultUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 统计报表  前端控制器
 * </p>
 *
 * @author xiaozhang
 * @since 2022-01-18
 */

@Api(tags = "统计报表")
@CrossOrigin
@RestController
@RequestMapping("/statement")
public class StatementController {


    @Autowired
    StatementService service;
    /**
     * 查看每个客户或每年的订单总额
     * */
    @Auth("SE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "客户的id"),
            @ApiImplicitParam(name = "date",value = "年")
    })
    @ApiOperation(value = "统计订单")
    @RequestMapping(value = "sum",method = RequestMethod.POST)
    public ResultUtil sum(@RequestParam(defaultValue = "0") Integer id, String date){

        List<Map<String, Object>> map=new ArrayList<Map<String, Object>>();;

         //显示全部
        map=service.countAll(id,date);


        // 有两个条件的显示错误信息

        if(id!=0 && date !=null){
            return ResultUtil.resMsgError("只能查询一个");
        }

        return ResultUtil.resObj(map);
    }


    /**
     * 按不同条件查看客户数量
     * */
    @Auth("SE")
    @ApiImplicitParam(name = "index",value = "查询条件")
    @ApiOperation(value = "按不同条件查看客户数量")
    @RequestMapping(value = "count",method = RequestMethod.POST)
    public ResultUtil count(@RequestParam(defaultValue = "等级") String index){



        return ResultUtil.resObj(service.findCountAmId(index));
    }

    /**
     *客户流失分析
     */
    @Auth("SE")
    @ApiImplicitParam(name = "id" , value = "客户经理的id")
    @ApiOperation(value = "查询客户流失情况")
    @RequestMapping(value = "loss",method = RequestMethod.POST)
    public ResultUtil loss(@RequestParam(defaultValue = "0") Integer id){
            return ResultUtil.resObj(service.loss(id));
    }


    /**
     *客户服务分析
     * */
    @Auth("SE")
    @ApiImplicitParam(name = "year",value = "输入年份查询")
    @ApiOperation(value = "客户服务分析")
    @RequestMapping(value = "yearService",method = RequestMethod.POST)
    public ResultUtil yearService(String year){

        return ResultUtil.resObj(service.servicesCount(year));
    }

}
